/**
 * 个人统计分析
 */

//页面载入初始化
$( "body" ).ready( function( e )
{
	try
	{
		if( typeof ( eval( "init" ) ) == "function" )
			eval( "init();" );
	}
	catch( e )
	{
	}

	// 初始化
	restat();
} );

//窗口大小改变
$( window ).resize( function( e )
{
	restat();
} );

function restat()
{
	$( "[id^='tbl_client']" ).each( function( idx, el )
	{
		resize_stat_bar( $( el ) );
	} );

	$( "[id^='tbl_reserve']" ).each( function( idx, el )
	{
		resize_stat_bar( $( el ) );
	} );

	$( "[id^='tbl_reception']" ).each( function( idx, el )
	{
		resize_stat_bar( $( el ) );
	} );

}

//--------------------------------------------------------------------------
//
//	统计方法
//
//--------------------------------------------------------------------------

// 统计条的状态条颜色
var colors = [
		"#BF98BE", "#D8A5CC", "#E2CCE5", "#EFE5EB", "#DFDFF4", "#D7EE91"
];

function resize_stat_bar( tbl )
{
	var root = tbl.find( ".statbar[rel='root']" );
	var rw = root.width();
	if( root.text() == "0" )
	{
		rw = 30;
		root.width( rw );
	}

	tbl.find( ".statbar" ).each( function( id )
	{
		$( this ).css( {
			"background": colors[ id ]
		} );
	} );

	tbl.find( ".statbar[rel!='root']" ).each( function( id )
	{
		fill_stat_bar( $( this ), id, rw );
	} );
}

function fill_stat_bar( sp, id, rw )
{
	var sa = sp.attr( "rel" ).split( "/" );
	var a = parseInt( sa[ 0 ] );
	var t = parseInt( sa[ 1 ] );
	var p = ( t > 0 ? Math.round( a / t * 10000 ) / 100 : 0 ) + "%";

	sp.hasClass( "percent" ) ? sp.text( p ) : sp.text( a );
	var w = rw * ( a / t );
	sp.width( w < 30 || isNaN( w ) ? 30 : w );

	if( !sp.hasClass( "percent" ) )
		sp.hover( function()
		{
			var sa = $( this ).attr( "rel" ).split( "/" );
			var a = parseInt( sa[ 0 ] );
			var t = parseInt( sa[ 1 ] );
			var p = ( t > 0 ? Math.round( a / t * 10000 ) / 100 : 0 ) + "%";
			$( this ).text( p );
		}, function()
		{
			var sa = $( this ).attr( "rel" ).split( "/" );
			var a = parseInt( sa[ 0 ] );
			$( this ).text( a );
		} );
}
